package toXML;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import person.DayList;
import person.DayPlan;
import person.Interval;
import person.Person;
import person.PersonAchive;
import person.Sex;
import sensor.Sensor;
import sensor.SensorType;

public class LoadFamilyXML {

	private static Person buildPerson(Node item) {
		Person p = new Person();
		NamedNodeMap attributeList = item.getAttributes();
		/**
		 * Reading Attributes from list;
		 */
		Node first = attributeList.getNamedItem("FirstName");
		Node last = attributeList.getNamedItem("LastName");
		Node sex = attributeList.getNamedItem("sex");
		Node age = attributeList.getNamedItem("age");
		Node rf = attributeList.getNamedItem("RFID_ID");
		Sensor s = new Sensor(Integer.parseInt(rf.getNodeValue()));
		s.setType(SensorType.RF_SENSOR);
		p.setrFid(s);
		p.setName(first.getNodeValue());
		p.setLastName(last.getNodeValue());
		p.setSex(Sex.match(sex.getNodeValue()));
		p.setAge(Integer.parseInt(age.getNodeValue()));
		NodeList list = item.getChildNodes();
		if (list != null) {
			for (int i = 0; i < list.getLength(); i++)
				if (list.item(i).getNodeName().equalsIgnoreCase("Schedule")) {
					list = list.item(i).getChildNodes();
					DayPlan dp = new DayPlan();
					for (int j = 0; j < list.getLength(); j++) {
						attributeList = list.item(j).getAttributes();
						if (isDay(list.item(j).getNodeName())) {
							String leave = attributeList.getNamedItem(
									"LeaveTime").getNodeValue();
							String home = attributeList
									.getNamedItem("HomeTime").getNodeValue();
							dp.setPlan(list.item(j).getNodeName(),
									new Interval(leave, home));
						}
					}
					p.setPlan(dp);
					break;
				}
		}
		return p;
	}

	private static boolean isDay(String nodeName) {
		for (int i = 0; i < DayList.days.length; i++)
			if (DayList.days[i].equals(nodeName))
				return true;
		return false;
	}

	public static void loadXML(File f) {

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(f);
			doc.getDocumentElement().normalize();

			NodeList nodeList = doc.getElementsByTagName("Family");
			nodeList = doc.getElementsByTagName("Person");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Person nextPerson = buildPerson(nodeList.item(i));
				PersonAchive.add(nextPerson);
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
